package com.budwk.app.sqfwV2.generator.baiyunqv;

import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.FamilyInformation;
import com.budwk.app.sqfwV2.dto.baiyunqu.OldPeopleSeekPdfDTO;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.policy.RenderPolicy;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-03-12
 * <p>
 * 白云区特殊困难老年人探访 OldPeopleSeek
 * 特殊困难老年人探访关爱服务记录表.docx
 */
public class OldPeopleSeekPdfGenerator extends BasePdfGenerator {

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        return true;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "baiyunqv/oldPeopleSeek.docx";
    }

    public String[] getListKey() {
        String lists[] = {"familyInformation"};
        return lists;
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        OldPeopleSeekPdfDTO dto = (OldPeopleSeekPdfDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        // 网格员签名
        String signatureName = "signature";
        Object signature = paramMap.get(ClassUtil.getField(CommonParam::getCreateUserSigUrl));
        List<String> signatureList = Arrays.asList(signature == null ? "" : signature.toString());
        List<Map<String, PictureRenderData>> signatures = CommonFormOfficeUtils.addWordPicture(signatureList, signatureName, 1.0F);
        paramMap.put(signatureName + "s", signatures);
        if (signature != null && StringUtils.isNotBlank(signature.toString())) {
            paramMap.put("createUserName", "");
        }

        // 被检查者签名
        String checkedUnitLeader = ClassUtil.getField(OldPeopleSeekPdfDTO::getCheckedUnitLeader);
        List<Map<String, PictureRenderData>> checkedUnitLeaders =
                CommonFormOfficeUtils.addWordPicture(Arrays.asList(dto.getCheckedUnitLeader()), checkedUnitLeader, 1.0F);
        paramMap.put(checkedUnitLeader + "s", checkedUnitLeaders);

        // 时间
        Date inspectionDate = dto.getInspectionDate();
        paramMap.put("yyyy", DateUtil.year(inspectionDate));
        paramMap.put("mm", DateUtil.month(inspectionDate) + 1);//月份从0开始,所以要加1
        paramMap.put("dd", DateUtil.dayOfMonth(inspectionDate));

        // 家庭收入,分转元
        Long monthlyHouseholdIncome = dto.getMonthlyHouseholdIncome();
        Object money = monthlyHouseholdIncome == null ? "" : BigDecimal.valueOf(monthlyHouseholdIncome).divide(new BigDecimal(100));
        paramMap.put(ClassUtil.getField(OldPeopleSeekPdfDTO::getMonthlyHouseholdIncome), money);

        // 家庭成员
        paramMap.put(getListKey()[0], getFamilyInformation(dto));

        // 自定义字段
        paramMap.putAll(pdfGenerateParam.getCustomerParamMap());
    }

    protected Map<String, RenderPolicy> createCustomerRenderPolicy(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        Map<String,RenderPolicy> renderPolicyMap = Maps.newHashMap();
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        renderPolicyMap.put(getListKey()[0], policy);
        return renderPolicyMap;
    }

    public static List<Map<String, Object>> getFamilyInformation(OldPeopleSeekPdfDTO dto) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<FamilyInformation> familyInformationList = dto.getFamilyInformation();
        if (familyInformationList != null && familyInformationList.size() > 0) {

            int i = 0;
            for (FamilyInformation familyInformation : familyInformationList) {
                if ("本人".equals(familyInformation.getRelationshipWithHouseholder())) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("num", "户主");
                    map.put(ClassUtil.getField(FamilyInformation::getName), familyInformation.getName());
                    map.put(ClassUtil.getField(FamilyInformation::getSex), familyInformation.getSex());
                    map.put(ClassUtil.getField(FamilyInformation::getIDNumber), familyInformation.getIDNumber());
                    map.put(ClassUtil.getField(FamilyInformation::getRelationshipWithHouseholder), familyInformation.getRelationshipWithHouseholder());
                    map.put(ClassUtil.getField(FamilyInformation::getPhysicalCondition), familyInformation.getPhysicalCondition());
                    Boolean peopleInExtremePoverty = familyInformation.getPeopleInExtremePoverty();
                    Object extremePoverty = peopleInExtremePoverty == null ? "" : peopleInExtremePoverty ? "是" : "否";
                    map.put(ClassUtil.getField(FamilyInformation::getPeopleInExtremePoverty), extremePoverty);

                    mapList.add(map);

                    break;
                } else {
                    i ++;
                }
            }
            if (i == familyInformationList.size()) mapList.add(getMap("户主"));

            int num = 1;
            for (FamilyInformation familyInformation : familyInformationList) {
                if ("本人".equals(familyInformation.getRelationshipWithHouseholder())) continue;
                num ++;
                Map<String, Object> map = new HashMap<>();
                map.put("num", num);
                map.put(ClassUtil.getField(FamilyInformation::getName), familyInformation.getName());
                map.put(ClassUtil.getField(FamilyInformation::getSex), familyInformation.getSex());
                map.put(ClassUtil.getField(FamilyInformation::getIDNumber), familyInformation.getIDNumber());
                map.put(ClassUtil.getField(FamilyInformation::getRelationshipWithHouseholder), familyInformation.getRelationshipWithHouseholder());
                map.put(ClassUtil.getField(FamilyInformation::getPhysicalCondition), familyInformation.getPhysicalCondition());
                Boolean peopleInExtremePoverty = familyInformation.getPeopleInExtremePoverty();
                Object extremePoverty = peopleInExtremePoverty == null ? "" : peopleInExtremePoverty ? "是" : "否";
                map.put(ClassUtil.getField(FamilyInformation::getPeopleInExtremePoverty), extremePoverty);

                mapList.add(map);
            }
            if (num == 1) {
                mapList.add(getMap("2"));
                mapList.add(getMap("3"));
            }
            if (num == 2) {
                mapList.add(getMap("3"));
            }
        } else {
            mapList.add(getMap("户主"));
            mapList.add(getMap("2"));
            mapList.add(getMap("3"));
        }

        return mapList;
    }

    private static Map<String, Object> getMap(Object num) {
        Map<String, Object> map = new HashMap<>();
        map.put("num", num);
        map.put(ClassUtil.getField(FamilyInformation::getName), "");
        map.put(ClassUtil.getField(FamilyInformation::getSex), "");
        map.put(ClassUtil.getField(FamilyInformation::getIDNumber), "");
        map.put(ClassUtil.getField(FamilyInformation::getRelationshipWithHouseholder), "");
        map.put(ClassUtil.getField(FamilyInformation::getPhysicalCondition), "");
        map.put(ClassUtil.getField(FamilyInformation::getPeopleInExtremePoverty), "");
        return map;
    }
}
